package com.czd.support.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.czd.base.model.Paginate;
import com.czd.support.common.Configuration;
import com.czd.support.model.Manager;
import com.czd.support.model.Video;
import com.czd.support.service.VideoService;

/**
 * @Description 视频上传controller
 * @author caoren
 * 
 */
@Controller
@RequestMapping(value = "/video")
public class VideoController extends BaseController{
	
	@Autowired
	private VideoService videoService;
	
	/**
	 * 视频列表
	 * @param model
	 * @param video
	 * @param pageNo
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list(Model model,Video video,@RequestParam(value = "pageNo",defaultValue = "1") int pageNo){
		video.setPageSize(Configuration.getInstance().getDefault_page_size());
		video.setPageNo(pageNo);
		Paginate paginate=videoService.queryForPage(video);
		model.addAttribute(paginate);
		return "/video/list";
	}
	
	@RequestMapping(value = "add")
	public String add(){
		return "/video/add";
	}
	
	/**
	 * 录入视频
	 * @param video
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/videoUpload")
	public String videoUpload(Video video,HttpServletRequest request,Model model){
		Manager manager=this.getSesionUser(request);
		video.setCreator(manager.getUserCode());
		video.setUpdator(manager.getUserCode());
		if(videoService.insert("saveVideo", video)){
			return "redirect:/video/list";
		}
		return error(model, "操作提示", "保存失败");
	}
	
	/**
	 * 编辑视频页面跳转
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable int id,Model model){
		Video video = videoService.findById(id);
		if(video != null){
			model.addAttribute("video", video);
			return "/video/edit";
		}
		return error(model, "视频不存在", "视频不存在");
	}
	
	/**
	 * 编辑视频
	 * @param request
	 * @param video
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public String edit(HttpServletRequest request, Video video, Model model){
		Manager manager = this.getSesionUser(request);
		video.setUpdator(manager.getUserCode());
		if(videoService.update(video)){
			return "redirect:/video/list";
		}
		return error(model, "编辑失败", "编辑失败");
	}
}
